using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
	public class AccessoryData
	{
		#region Fields
		protected static SqlConnection dbConn = new SqlConnection(Config.Global.Settings.ConnectionString);
		#endregion

		#region Get Object Method
		private static object getNullObject(object obj)
		{
			return obj == null ? DBNull.Value : obj;
		}
		private static object getNullValue(object obj)
		{
			return obj == DBNull.Value ? null : obj;
		}
		#endregion

		#region Constructors
		public AccessoryData() {}
		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Accessory table.
		/// </summary>
		/// <param name="accessoryInfo"></param>
		/// <returns>ID of Saved record</returns>
		public static int? Insert(AccessoryInfo accessoryInfo)
		{
			SqlCommand dbCmd = new SqlCommand("[uspAccessoryInsert]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@SiteID", getNullObject(accessoryInfo.SiteID));
			dbCmd.Parameters.AddWithValue("@AccessoryName", getNullObject(accessoryInfo.AccessoryName));
			dbCmd.Parameters.AddWithValue("@AccessoryDescription", getNullObject(accessoryInfo.AccessoryDescription));
			dbCmd.Parameters.AddWithValue("@AccessoryUnit", getNullObject(accessoryInfo.AccessoryUnit));
			dbCmd.Parameters.AddWithValue("@AccessoryPrice", getNullObject(accessoryInfo.AccessoryPrice));
			dbCmd.Parameters.AddWithValue("@AccessoryStatus", getNullObject(accessoryInfo.AccessoryStatus));
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value);
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Updates a record in the Accessory table.
		/// </summary>
		/// <param name="accessoryInfo"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool Update(AccessoryInfo accessoryInfo)
		{
			SqlCommand dbCmd = new SqlCommand("[uspAccessoryUpdate]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@AccessoryID", getNullObject(accessoryInfo.AccessoryID));
			dbCmd.Parameters.AddWithValue("@SiteID", getNullObject(accessoryInfo.SiteID));
			dbCmd.Parameters.AddWithValue("@AccessoryName", getNullObject(accessoryInfo.AccessoryName));
			dbCmd.Parameters.AddWithValue("@AccessoryDescription", getNullObject(accessoryInfo.AccessoryDescription));
			dbCmd.Parameters.AddWithValue("@AccessoryUnit", getNullObject(accessoryInfo.AccessoryUnit));
			dbCmd.Parameters.AddWithValue("@AccessoryPrice", getNullObject(accessoryInfo.AccessoryPrice));
			dbCmd.Parameters.AddWithValue("@AccessoryStatus", getNullObject(accessoryInfo.AccessoryStatus));
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Deletes a record from the Accessory table by its primary key.
		/// </summary>
		/// <param name="accessoryID"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool DeleteByAccessoryID(int accessoryID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspAccessoryDeleteByAccessoryID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@AccessoryID", accessoryID);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Deletes a record from the Accessory table by a foreign key.
		/// </summary>
		/// <param name="siteID"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool DeleteAllBySiteID(int siteID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspAccessoryDeleteAllBySiteID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@SiteID", siteID);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects a single record from the Accessory table.
		/// </summary>
		/// <param name="accessoryID"></param>
		/// <returns>AccessoryInfo: A record from the Accessory table by its primary key.</returns>
		public static AccessoryInfo SelectByAccessoryID(int accessoryID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspAccessorySelectByAccessoryID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			AccessoryInfo accessoryInfo =  new AccessoryInfo();

			dbCmd.Parameters.AddWithValue("@AccessoryID", accessoryID);

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.Read())
				{
					accessoryInfo.AccessoryID = System.Convert.ToInt32(getNullValue(dataReader["AccessoryID"]));
					accessoryInfo.SiteID = System.Convert.ToInt32(getNullValue(dataReader["SiteID"]));
					accessoryInfo.AccessoryName = System.Convert.ToString(getNullValue(dataReader["AccessoryName"]));
					accessoryInfo.AccessoryDescription = System.Convert.ToString(getNullValue(dataReader["AccessoryDescription"]));
					accessoryInfo.AccessoryUnit = System.Convert.ToString(getNullValue(dataReader["AccessoryUnit"]));
					accessoryInfo.AccessoryPrice = System.Convert.ToDouble(getNullValue(dataReader["AccessoryPrice"]));
					accessoryInfo.AccessoryStatus = System.Convert.ToBoolean(getNullValue(dataReader["AccessoryStatus"]));
				}

				dataReader.Close();
				return accessoryInfo;
			}
			catch (Exception)
			{
				return new AccessoryInfo();
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects all records from the Accessory table.
		/// </summary>
		/// <return>DataTable</return>
		public static DataTable SelectAll()
		{
			SqlCommand dbCmd = new SqlCommand("[uspAccessorySelectAll]",dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			DataTable dtbAccessory =  new DataTable();

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.HasRows)
				{
					dtbAccessory.Load(dataReader);
				}

				dataReader.Close();
				return dtbAccessory;
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects all records from the Accessory table by a foreign key.
		/// </summary>
		/// <param name="siteID"></param>
		/// <return>All records from the Accessory table by a foreign key.</return>
		public static DataTable SelectAllBySiteID(int siteID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspAccessorySelectAllBySiteID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			DataTable dtbAccessory =  new DataTable();

			dbCmd.Parameters.AddWithValue("@SiteID", siteID);

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.HasRows)
				{
					dtbAccessory.Load(dataReader);
				}

				dataReader.Close();
				return dtbAccessory;
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		#endregion
	}
}
